package com.course.server.service;

import com.course.server.domain.Member;
import com.course.server.domain.MemberExample;
import com.course.server.dto.LoginMemberDto;
import com.course.server.dto.MemberDto;
import com.course.server.dto.PageDto;
import com.course.server.exception.BusinessException;
import com.course.server.exception.BusinessExceptionCode;
import com.course.server.mapper.MemberMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;



@Service
public class MemberService {
    private  static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Resource
    private MemberMapper memberMapper;
    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        MemberExample memberExample = new MemberExample();
        List<Member> memberList = memberMapper.selectByExample(memberExample);
        PageInfo<Member> pageInfo = new PageInfo<>(memberList);
        pageDto.setTotal(pageInfo.getTotal());
        List<MemberDto> memberDtoList = CopyUtil.copyList(memberList,MemberDto.class);
        pageDto.setList(memberDtoList);
    }

    /**
     * 保存 id有更新的时候更新 无值的时候 新增
     * @param memberDto
     */
    public void save(MemberDto memberDto){
        Member member = CopyUtil.copy(memberDto,Member.class);
        if(StringUtils.isEmpty(memberDto.getId())){
            this.insert(member);
        }else{
            this.update(member);
        }
    }

    /**
    * 更新
    */
    private void update(Member member) {
    memberMapper.updateByPrimaryKey(member);
    }



    /**
     * 新增
     * @param member
     */
    private void insert(Member member){
        Date now = new Date();
        member.setId(UuidUtil.getShortUuid());
        memberMapper.insert(member);
    }
        /**
         * 删除
         */
    public void delete(String id){
        memberMapper.deleteByPrimaryKey(id);
    }
    /**
     * 按手机号查找
     *
     */
     public  Member selectByMobile (String mobile){
         if (StringUtils.isEmpty(mobile)){
             return null;
         }
         MemberExample example = new MemberExample();
         example.createCriteria().andMobileEqualTo(mobile);
         List<Member> memberList = memberMapper.selectByExample(example);
         if (memberList==null||memberList.size()==0){
             return  null;
         }else {
             return  memberList.get(0);
         }
     }
    /**
     * 登录
     */
    public LoginMemberDto login(MemberDto memberDto){
        Member member = selectByMobile(memberDto.getMobile());
        if(member==null) {
            LOG.info("手机号不存在，{}", memberDto.getMobile());
            throw new BusinessException(BusinessExceptionCode.LOGIN_MEMBER_ERROR);
        }else{
            if (member.getPassword().equals(memberDto.getPassword())){
                //登录成功
                LoginMemberDto loginMemberDto=CopyUtil.copy(member,LoginMemberDto.class);
                return loginMemberDto;

            }else{
                LOG.info("密码不对，请输入密码：{},数据库密码：{}",memberDto.getPassword(),member.getPassword());
                throw  new BusinessException(BusinessExceptionCode.LOGIN_MEMBER_ERROR);
            }

        }
    }

    /**
     * 按手机号查找
     * @param mobile
     * @return
     */
    public MemberDto findByMobile(String mobile) {
        Member member = this.selectByMobile(mobile);
        return CopyUtil.copy(member,MemberDto.class);
    }

    /**
     * 重置密码
     * @param memberDto
     */
    public void resetPassword(MemberDto memberDto) throws BusinessException{
        Member memberDb = this.selectByMobile(memberDto.getMobile());
        if(memberDb==null){
            throw new BusinessException(BusinessExceptionCode.MEMBER_NOT_EXIST);

        }else{
            Member member = new Member();
            member.setId(memberDb.getId());
            member.setPassword(memberDto.getPassword());
            memberMapper.updateByPrimaryKeySelective(member);
        }
    }
}